<?php
namespace Api\Controller;
use Think\Controller;

class MapController extends BaseController {

   private $m = NULL;
   private $md = NULL;
   private $navName = '订单管理';


   public function _initialize(){
   		parent::_initialize();
	        //$this->m = M('shop_cart');
    }
    
              
  //计算范围，可以做搜索用户  lng 经度 lat纬度  distance半径用 单位千米
public function nearbytask(){
         $lng=I('lng');
         $lat=I('lat'); 
         $distance = I('distance'); 
         $radius = 6371;
         $dlng = 2 * asin(sin($distance / (2 * $radius)) / cos(deg2rad($lat)));
         $dlng = rad2deg($dlng);
         $dlat = $distance / $radius;
         $dlat = rad2deg($dlat);
         $maxLat=$lat + $dlat;
         $minLat=$lat - $dlat;
         $maxLng=$lng + $dlng;
         $minLng=$lng - $dlng;
         $w_lon_Range['t_lng']=array('between',"$minLng,$maxLng");
         $w_lon_Range['t_lat']=array('between',"$minLat,$maxLat");
         $info= M('task')
         ->where($w_lon_Range)->select();

         foreach ($info as $key => $value) { 
            $info[$key]['distance']= $this->getDistance($value['t_lat'],$value['t_lng'],$lat,$lng);
         }
            $flag=array();
            foreach($info as $arr2){
                $flag[]=$arr2["distance"];
                }
          array_multisort($flag, SORT_ASC, $info);
          
          foreach ($info as $key => $value) {
            if($info[$key]['distance']<10000){ //小于10000 就用米来 做长度单位
            $info[$key]['distance']= ((int)($info[$key]['distance'])).'米';
            }
            if($info[$key]['distance']>=10000){ //大于10000 就用公里来 做长度单位
            $info[$key]['distance']= ((int)($info[$key]['distance']/1000)).'公里内';
            }
          }
           if($info){
            $date['info']=$info;
            $date['status']=100;
            $lists->date=$date;
            $this->ajaxReturn($lists);
           }else{
            $date['info']=$info;
            $date['status']=101;
            $lists->date=$date;
            $this->ajaxReturn($lists);
           }
     }


  /** 
*  @desc 根据两点间的经纬度计算距离 
*  @param float $lat 纬度值 
*  @param float $lng 经度值 
*/  
 function getDistance($lat1, $lng1, $lat2, $lng2)  
 {  
     $earthRadius =6371393; //approximate radius of earth in meters  
     /* 
       Convert these degrees to radians 
       to work with the formula 
     */  
     $lat1 = ($lat1 * pi() ) / 180;  
     $lng1 = ($lng1 * pi() ) / 180;  
   
     $lat2 = ($lat2 * pi() ) / 180;  
     $lng2 = ($lng2 * pi() ) / 180;
     /* 
       Using the 
       Haversine formula 
       http://en.wikipedia.org/wiki/Haversine_formula 
       calculate the distance 
     */  
     $calcLongitude = $lng2 - $lng1;  
     $calcLatitude = $lat2 - $lat1;  
     $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);    
     $stepTwo = 2 * asin(min(1, sqrt($stepOne)));  
     $calculatedDistance = $earthRadius * $stepTwo;  
     return (int)$calculatedDistance;  
 }
   
    
}

 
  